package com.yhl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhl.entity.Provider;
import com.yhl.mapper.ProviderMapper;
import com.yhl.service.ProviderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author YaoHongli
* @since 2023-03-17 17:45:59
*/
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProviderServiceImpl implements ProviderService {
    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public void addProvider(Provider provider){
        providerMapper.insert(provider);
    }
    @Override
    public void deleteProvider(String id){
        providerMapper.deleteById(id);
    }
    @Override
    public void updateProvider(Provider provider){
        providerMapper.updateById(provider);
    }
    @Override
    public Provider getProviderById(String id){
        return providerMapper.selectById(id);
    }
    @Override
    public IPage<Provider> getProviderByPage(Page page, Provider provider){
        LambdaQueryWrapper<Provider> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(provider.getProCode())){
            lambdaQueryWrapper.like(Provider::getProCode,provider.getProCode());
        }
        if(StringUtils.isNotEmpty(provider.getProName())){
            lambdaQueryWrapper.like(Provider::getProName,provider.getProName());
        }
        IPage<Provider> providerIPage = providerMapper.selectPage(page,lambdaQueryWrapper);
        return providerIPage;
    }

    @Override
    public List<Provider> getProviderList() {
        QueryWrapper<Provider> providerQueryWrapper = new QueryWrapper<>();
        return providerMapper.selectList(providerQueryWrapper);
    }
}

